package csm.sql.pool;

import java.sql.Connection;
import java.sql.SQLException;

import oracle.jdbc.pool.OracleConnectionCacheImpl;
import oracle.jdbc.pool.OracleConnectionPoolDataSource;

import csm.common.TypeConv;
import csm.sql.ConnectionFactory;
import csm.sql.DBConfig;
import csm.sql.SmartConnection;
/**
 * 오라클 커넥션 풀
 * @author seungmun
 * @since 2006.1.18.
 * @version 
 * @see
 */
public class OracleConnectionPool extends ConnectionFactory
{
	OracleConnectionCacheImpl occi;    
	
	public OracleConnectionPool()
	{
	}
	
	@Override
	protected void createConnectionPool() throws SQLException {
		try 
		{
			if( occi != null )
			{
				// 전혀 다른 설정으로 풀을 생성할 수 있기 때문에...
				closeConnectionPool();
			}
			
			occi = new OracleConnectionCacheImpl();
			OracleConnectionPoolDataSource ocpds = new OracleConnectionPoolDataSource();
			
			DBConfig dbc = DBConfig.getDBConfig(this.connectionName);

			ocpds.setURL(dbc.getSettings("connection.url"));
			ocpds.setUser(dbc.getSettings("connection.user"));
			ocpds.setPassword(dbc.getSettings("connection.password"));
			occi.setConnectionPoolDataSource(ocpds);
			occi.setMaxLimit(TypeConv.ToInt( dbc.getSettings("connection.maximumPoolSize")));
			occi.setMinLimit(TypeConv.ToInt( dbc.getSettings("connection.minimumPoolSize")));
		}
		catch (SQLException ex) {
			throw new SQLException(ex.toString() + " in OracleConnectionPool.createConnectionPool()", ex.getSQLState(),
					ex.getErrorCode());
		}
	}
	
	@Override
	public Connection getConnection() throws SQLException
	{
		try {
			if (occi == null)
				createConnectionPool();
			
			Connection conn = occi.getConnection();
			conn.setAutoCommit(true);
			
			return new SmartConnection(conn);
		}
		catch (SQLException ex) {
			throw new SQLException(ex.toString() + " in OracleConnectionPool.getConnection()", ex.getSQLState(), ex
					.getErrorCode());
		}
	}

	@Override
	public void closeConnectionPool() throws SQLException
	{
		occi.close();
		occi = null;		
	}



}
